home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Languages / Rewrite 0.2.6 / ReWrite 0.2.6 Docs / ReWrite 0.2.6 Docs.rsrc / TEXT_134.txt < prev    next >
Encoding:
Text File  |  1995-08-23  |  4.4 KB  |  192 lines

  1.  
  2. Chapter 6 - Numerical, Boolean
  3.  
  4. This sections deals with the functions designed to work on atoms (integers), including integer, boolean and bitwise functions.
  5.  
  6. Numerical Operations
  7.  
  8.    One thing worth noting about the n-ary numerical functions is that they can be called with zero or one argument.
  9.    If an n-ary function is called with one argument, it just returns that one argument.
  10. If an n-ary function is called with zero arguments, it returns some sort of identity for that function. For example:
  11.  
  12.   add[6] -> 6;
  13.   mul[] -> 1;
  14.  
  15. Basic Arithmetic
  16.  
  17. ReWrite supports the following operations, all of which work on integers. The usual precedence rules apply.
  18.  
  19. add[ints ]  or  int + int  -> int ;
  20. mul[ints ]  or  int * int  -> int ;
  21. neg[int ]  or  -int  -> int ;
  22. sub[int ,int ]  or  int - int  -> int ;
  23. div[int ,int ]  or  int / int  -> int ;
  24. mod[int ,int ]  or  int % int  -> int ;
  25. (all directly coded)
  26.  
  27. Example:
  28.   2 + 127 % 109  ->  9
  29.  
  30. In addition, the following comparison operators are given for integers and characters (don't forget that eq, ne are also defined, but for general values rather than just integers).
  31.  
  32. Comparison
  33.  
  34. ge[int ,int ]  or  int >= int  -> bool ;
  35. gt[int ,int ]  or  int > int  -> bool ;
  36. le[int ,int ]  or  int <= int  -> bool ;
  37. lt[int ,int ]  or  int < int  -> bool ;
  38. (all directly coded)
  39.  
  40. ge[char ,char ]  or  char >= char  -> bool ;
  41. gt[char ,char ]  or  char > char  -> bool ;
  42. le[char ,char ]  or  char <= char  -> bool ;
  43. lt[char ,char ]  or  char < char  -> bool ;
  44. (not directly coded)
  45.  
  46. Example:
  47.   2 + 127 % 109 > 8  ->  true
  48.  
  49. There are several other numerical functions supported:
  50.  
  51. Absolute value
  52.  
  53. abs[int ] -> int ;
  54.  
  55.   abs[x:int]::x>=0 -> x;
  56.   abs[x:int] -> -x;
  57.  
  58. max, min
  59.  
  60. max[ints ] -> int ;
  61.  
  62.   max[] -> minint;
  63.   max[x:int] -> x;
  64.   max[x:int,y:int,.rest]::x>=y -> max[x,.rest];
  65.   max[x:int,y:int,.rest] -> max[y,.rest];
  66.  
  67. min[ints ] -> int ;
  68.  
  69.   min[] -> maxint;
  70.   min[x:int] -> x;
  71.   min[x:int,y:int,.rest]::x<=y -> min[x,.rest];
  72.   min[x:int,y:int,.rest] -> min[y,.rest];
  73.  
  74. Note that these require the arguments not to be in a list.
  75. For example:
  76.   max[1,2,6,3] -> 6;
  77.   max[{1,2,6,3}] -> fails
  78. If you want to find the largest number in the list lis, use max[.lis].
  79.  
  80. maxint, minint
  81.  
  82. These functions just provide easy access to these constants. Note that I consider minint to be -2,147,483,647, not -2,147,483,648.
  83.  
  84. maxint[] -> $7FFFFFFF;
  85.  
  86.   maxint[] -> $7FFFFFFF;
  87.  
  88. minint[] -> -$7FFFFFFF;
  89.  
  90.   minint[] -> -$7FFFFFFF;
  91.  
  92. random, Uniqnum, tickcount
  93.  
  94. random returns a pseudo-random integer in the range -32767 to 32767. See Inside Macintosh, Vol I, page 194.
  95.  
  96. random[] -> int ;
  97. (directly coded)
  98.  
  99. Logical Operations
  100.  
  101. These are the usual logical operations.
  102.  
  103. and[bools ]  or  bool & bool  -> bool ;
  104.  
  105.   and[] -> true;
  106.   and[false,.rest] -> false;
  107.   and[x:bool,.rest] -> and[.rest];
  108.  
  109. or[bools ]  or  bool | bool  -> bool ;
  110.  
  111.   or[] -> false;
  112.   or[false,.rest] -> or[.rest];
  113.   or[x:bool,.rest] -> x;
  114.  
  115. not[bool ]  or  !bool  -> bool ;
  116.  
  117.   not[false] -> true;
  118.   not[true] -> false;
  119.  
  120. Bit Operations
  121.  
  122. bitand, bitor, bitnot
  123.  
  124. These are just the typical bit operations, treating an integer as 32 bits
  125.  
  126. bitand[ints ] -> int ;
  127. bitor[ints ] -> int ;
  128. bitnot[int ] -> int ;
  129. (directly coded)
  130.  
  131. For example:
  132.   bitand[%1011,%01011101] -> 9;  (%1001)
  133.  
  134. hi, lo
  135.  
  136. Given x  an integer consists of 32 bits:
  137. hi[x ] returns the most significant 16 bits (shifted right by 16 bits),
  138. lo[x ] returns the least significant 16 bits.
  139. This function is mostly used by the assembler.
  140.  
  141. hi[int ] -> int ;
  142. lo[int ] -> int ;
  143. (directly coded)
  144.  
  145. shift
  146.  
  147. shift[x ,y ] shifts the integer y left by x bits - effectively it returns y*2^x
  148.  
  149. shift[int ,int ] -> int ;
  150. (directly coded)
  151.  
  152. Examples:
  153.   shift[3,100] -> 800;
  154.   shift[-3,800] -> 12;
  155.  
  156. bitff1, bitff0, bitcount
  157.  
  158. bitff1 and bitff0 are somewhat anomalous functions. They are only documented here because they are used later for the definition of sets.
  159.  
  160. If an integer (considered in binary) is indexed the following way:
  161.  ¬†
  162.  
  163.  
  164.  
  165.  
  166. then bitff1 scans through from the most significant to the least and returns the index of the first 1 that it finds. bitff0 similarly looks for a 0.
  167.  
  168. bitff1[int ] -> int ;
  169. (directly coded)
  170.  
  171. bitff0[int ] -> int ;
  172.  
  173.   bitff0[x:int] -> bitff1[bitnot[x]];
  174.  
  175. Examples:
  176.   bitff1[100] -> 25;
  177.   bitff0[100] -> 0;
  178.   bitff1[0] -> 32;
  179.  
  180. To create a simple integer log base 2 function, we could do the following:
  181.  
  182.   log2[x:int] -> 31-bitff1[x];
  183.  
  184. bitcount returns the number of 1's in the binary representation of a number.
  185.  
  186. bitcount[int ] -> int ;
  187. (directly coded)
  188.  
  189. For example:
  190.   bitcount[%10101011] -> 5;
  191.  
  192.